/*******************************************************************************
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 * |                                                                         |
 *    faint - The Face Annotation Interface
 * |  Copyright (C) 2007  Malte Mathiszig                                    |
 * 
 * |  This program is free software: you can redistribute it and/or modify   |
 *    it under the terms of the GNU General Public License as published by
 * |  the Free Software Foundation, either version 3 of the License, or      |
 *    (at your option) any later version.                                     
 * |                                                                         |
 *    This program is distributed in the hope that it will be useful,
 * |  but WITHOUT ANY WARRANTY; without even the implied warranty of         |
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * |  GNU General Public License for more details.                           |
 * 
 * |  You should have received a copy of the GNU General Public License      |
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * |                                                                         |
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 *******************************************************************************/

package pl.matt.opencv;

import java.io.File;

import pl.matt.opencv.model.Region;

/**
 * @author maltech
 * 
 */
public class OpenCVDetection {
	
	@SuppressWarnings("unused")
	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OpenCVDetection.class);

	private static final long serialVersionUID = 9040924330337550071L;

	private static final String[] CASCADES = { "haarcascade_frontalface_default.xml",
			"haarcascade_frontalface_alt.xml", "haarcascade_frontalface_alt_tree.xml",
			"haarcascade_frontalface_alt2.xml", "haarcascade_profileface.xml" };

	private String currentCascadeFile = CASCADES[0];
	private String cascadesDirectory = "/home/mateusz/priv/workspace/mgr/resources/cascades";

	private float scaleFactor = 1.1f;

	private int scanWindowSize;
	private String libraryPath = "/home/mateusz/priv/workspace/mgr/jni/opencv.so";

	/**
	 * Constructor.
	 * 
	 */
	public OpenCVDetection() {
		// Load native libraries
		System.load(libraryPath);

	}
	
	public OpenCVDetection(String currentCascadeFile) {
		this();
		this.currentCascadeFile = currentCascadeFile;
	}	

	public synchronized pl.matt.opencv.model.Region[] detectObject(String file, int scanWindowSize) {
		this.scanWindowSize = scanWindowSize;
		return detectFacesJNI(file, this);
	}

	private static native Region[] detectFacesJNI(String file, OpenCVDetection caller);

	public String getCurrentCascadeFile() {
		return currentCascadeFile;
	}

	public String getCascade() {
		return cascadesDirectory + File.separator + currentCascadeFile;
	}

	public void setCascade(String cascade) {
		this.currentCascadeFile = cascade;
	}

	public int getMinSize() {
		return scanWindowSize;
	}

	public void setScale(float scaleFactor) {
		this.scaleFactor = scaleFactor;
	}

	public float getScale() {
		return scaleFactor;
	}

	/*
	 * For testing purpose
	 */
	public static void main(String[] args) {
		OpenCVDetection detection = new OpenCVDetection();
		Region[] regions = detection.detectObject("/home/mateusz/priv/workspace/mgr/resources/images/testFaces/00000072.jpg", 100);
		for (Region region : regions) {
			System.out.println(region);
		}
		// [356,312] | width = 197 | height = 197 | angle = 0.0 
	}

}
